<template>
  <div class="login-wrap">
    <div class="ms-title"></div>
    <div class="ms-login">
      <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
        <el-form-item prop="userName">
          <el-input v-model="formData.userName" placeholder="username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="formData.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" :loading="loading" @click="submitForm('ruleForm')">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
  export default {
    data: function() {
      return {
        loading: false,
        formData: {
          userName: "",
          password: "",
        },
        rules: {
          userName: [
            { required: true, message: "请输入用户名", trigger: "blur" }
          ],
          password: [
            { message: "请输入密码", trigger: "blur" }
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
       this.$refs[formName].validate((valid) => {
         if (valid) {
           this.loading = true;
           var data = {
             "userName": this.formData.userName,
             "password":  this.formData.password
           };

           //发起请求
           this.$axios.post('/api/User/Login', data)
            .then((res) => {
              var data = res.data.data;
              //成功函数
              this.loading = false;
              // 设置token
              var access_token = data.access_token;
              localStorage.setItem('access_token', access_token);//本地存储token

              var user = {
                name: data.name,
                mobile: data.mobile
              }
              localStorage.setItem('user', JSON.stringify(user));
              this.$router.push('/Home');
              console.log(res.data);
            })
            .catch((err) => {this.loading = false}) //catch是错误函数
         } else {
           this.$alert("账号密码填写错误");
           return false;
         }
       });
      }
    },
    created() {
      if (localStorage.getItem("userName")) {
        this.$router.push("/Home");
      }
    }
  }

</script>
<style scoped>
  .login-wrap {
    position: relative;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("../static/img/admin-login-bg.jpg") 0 0 no-repeat;
    background-size: 100% 100%;
  }

  .ms-title {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    background: url("../static/img/logo.png") 0 0 no-repeat;
    text-align: center;
    font-size: 30px;
    color: #fff;
  }

  .ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 300px;
    height: 180px;
    margin: -150px 0 0 -190px;
    padding: 40px;
    border-radius: 5px;
    background: #fff;
  }

  .login-btn {
    text-align: center;
  }

  .login-btn button {
    width: 100%;
    height: 50px;
  }

</style>
